import { getToken } from '@/common/auth'
import useLayoutStore from '@/stores/layout'
import useUserStore from '@/stores/user'
import NProgress from 'nprogress'

export async function loginGuard(to, from) {
  NProgress.start()
  const isAuth = to.meta.auth ?? true
  const token = getToken()
  if (!isAuth) {
    NProgress.done()
    return true
  }
  if (!token) {
    NProgress.done()
    return '/auth/login'
  }
  const userStore = useUserStore()
  await userStore.getUserInfo()
}

//根据路由配置初始化layoutStore
export function initLayoutGuard(to, from) {
  const layoutStore = useLayoutStore()
  layoutStore.pageTitle = to.meta.title
  return true
}
